import addSelButton from "@/components/diy-form/add-sel-button.vue";
import addSelBg from "@/components/diy-form/add-sel-bg.vue";
import shareWechat from '@/components/share/share-wechat.vue';
import detailOpelist from '@/components/modal/detail-opelist.vue';
import detailMenu from '@/components/modal/detail-menu.vue'
import { log10 } from "core-js/fn/number";
export default {
	components: {
		addSelBg,
		addSelButton,
		shareWechat,
		detailOpelist,
		detailMenu
	},
	data() {
		return {
			shareInfo: {},
			info: {},
			s_id: 0,
			loginStatus: false,
			buttonInfo: {
				show: false,
				bottonStatus: true,
				buttonText: '立即参加'
			},
			joinList: [],
			joinListEmpty: '',
			joinListOrderIndex: 0,
			joinListOrder: [{
					text: '时间降序 ↓',
					order: 'desc'
				},
				{
					text: '时间正序 ↑',
					order: 'asc'
				}
			],
			loginCallBackType: '',
		}
	},
	onLoad(params) {
		// alert(params.s_id)
		//params.s_id = 55; 
		if (params.s_id) {
			// alert(1)
			this.s_id = params.s_id;
			this.getEditInfo();
		}
		
	},
	onShow(){
		if (this.s_id) {
			
			// alert(2)
			this.getEditInfo();
		}
	},
	methods: {
		async wechatRegister(e) {
			if (e.code == 1) {
				var that = this;
				switch (that.loginCallBackType) {

					case 'submit':
						await that.getEditInfo();
						that.submit();
						break;
				}
				that.selUserIndex = '';
				that.loginCallBackType = '';
			}
		},
		shareFunc() {
			var info = this.info;
			var posterData = {
				title: info.title,
				nickname: info.nickname,
				qrcodeUrl: info.uni_qrcode,
				path: 'pages/form/statistics/detail?s_id=' + info.s_id
			};
			this.$refs.share_wechat.setShowStatus(true, posterData);
		},
		showOpe(index) {
			var that = this;
			var info = that.info;
			var data = {
				is_me: info.is_me,
				s_id: info.s_id,
				time_status: info.time_status,
				type: info.type,
				join_count: info.join_count,
				is_super_admin: info.is_super_admin
			};
			// this.$refs.detail_opelist.initData(data, function(res) {
				console.log('-----xx',this.$refs.detail_menu)
				console.log(data,'data')
				this.$refs.detail_menu.initData(data, function(res) {
				if (res.opeType == 'delete') {
					that.$api.navbackTo();
				} else if (res.opeType == 'setTime') {
					that.getEditInfo();
				}
			});
		},
		getEditInfo() {
			
			var that = this;
			if (that.s_id > 0) {
				that.$http.post('statistics/getEditInfo', {
					s_id: that.s_id,
					token:'eyJhbGciOiJTSEEyNTYiLCJ0eXAiOiJKV1QifQ==.eyJ0aW1lIjoxNzc2ODQ3MTE0LCJkYXRhIjp7ImFkbWluX2lkIjoxfX0=.yaiWERKtzS8FNa29DsjRHh04QIBgOfx1nThd7JvFEKQ='
				}).then(res => {
					if (res.code == 1) {
						var info = res.data.info;
						that.info = info;
						if (info.bgPath) {
							// that.$refs.add_sel_bg.initBgFunc(info.bgPath);
						}
						/* if(info.join_datalist)
						{
							that.formData = JSON.parse(info.join_datalist)
						} */
						// if (info.content_list) {
						// 	let join_contentlist = JSON.parse(info.content_list);
						// 	that.$refs.add_sel_button.initDataList(join_contentlist)
						// }
						// if (res.data.buttonInfo) {
						// 	that.buttonInfo = res.data.buttonInfo;
						// 	that.buttonInfo.show = true;
						// }
						// that.shareInfo = {
						// 	title: info.title,
						// 	s_id: info.s_id,
						// 	imageUrl: info.bgPath.qnPath
						// }
					
						//获取参加列表
						that.getJoinList();
						// 初始化 menu
						that.showOpe()
						
					} else {
						that.$api.msg(res.msg);
					}
				})
			}
		},
		getJoinList() {
			var that = this;
			that.$http.post('statistics/getJoinList', {
				s_id: that.s_id,
				orderby: that.joinListOrder[that.joinListOrderIndex].order
			}).then(res => {
				if (res.code == 1) {
					that.joinList = res.data.list;
					that.joinListEmpty = '';
				} else {
					that.joinList = [];
					that.joinListEmpty = res.msg;
				}
			})
		},
		changeOrder() {
			var that = this;
			var joinListOrder = that.joinListOrder;
			let itemList = [];
			joinListOrder.forEach(item => {
				itemList.push(item.text);
			})
			uni.showActionSheet({
				itemList: itemList,
				success(e) {
					var index = e.tapIndex;

					if (index.toString() != that.joinListOrderIndex.toString()) {
						that.joinListOrderIndex = index;
						that.$api.loadingMsg('');
						that.getJoinList();
					}
				}
			})
		},
		makePhone(phone) {
			this.$api.makePhone(phone);
		},
		joinDetailope(index, type) {
			var that = this;
			var info = that.joinList[index];
			switch (type) {
				case 'detail':
					this.$api.navigateTo('/pages/form/statistics/join-detail-show?join_id=' + info.join_id);
					break;
				case 'save':
					this.$api.navigateTo('/pages/form/statistics/join-detail-edit?join_id=' + info.join_id);
					break;
				case 'del':
					that.$api.showModal('确认删除', '删除后不可恢复', function(res) {
						if (res == 'yes') {
							that.delJoinInfo(info.join_id, index);
						}
					})
					break;
			}
		},
		delJoinInfo(join_id, index) {
			var that = this;
			that.$api.loadingMsg('正在删除');
			that.$http.post('statistics/joinStatisticsDelete', {
				join_id
			}).then(res => {
				that.$api.msg(res.msg);
				if (res.code == 1) {
					that.getEditInfo();
				}
			});
		},
		async submit() {
			var that = this;
			//判断是否登录
			var checkLogin = that.$api.checkLogin(false);
			if (checkLogin == false) {
				that.loginCallBackType = 'submit';
				that.$refs.wechat_login.saveShowStatus(true);
				return;
			}
			if (this.buttonInfo.bottonStatus == false) {
				return false;
			}
			if (that.info.join_datalist == '') {
				//直接参与
				that.$api.showModal('确认参加', '', async function(res) {
					if (res == 'yes') {
						var resData = '';
						that.$api.loadingMsg('');
						that.$http.post('statistics/joinStatistics', {
							resData,
							s_id: that.info.s_id
						}).then(res => {
							that.$api.msg(res.msg)
							if (res.code == 1) {
								that.getEditInfo();
							}
						})
					}
				})
			} else {
				that.$api.navigateTo('/pages/form/statistics/join?s_id=' + that.s_id);
			}

		}
	}
}